package cn.edu.neusoft.myappos.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.neusoft.myappos.Activity.CourseActivity;
import cn.edu.neusoft.myappos.Bean.CourseBean;
import cn.edu.neusoft.myappos.R;

public class CourseAdapter extends RecyclerView.Adapter {
    private  Context context;//上下文
    private LayoutInflater layoutInflater;//动态布局
    private List<CourseBean> list;//向rv中填充的数据
    //自定义 构造方法
    public CourseAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }

    //自定义 ViewHoder子类，容纳item视图
    public  class ViewHolder extends  RecyclerView.ViewHolder {
        ImageView imageView;
        TextView foodName,intro,shopName,price;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView);
            foodName=itemView.findViewById(R.id.n_Name);
            intro=itemView.findViewById(R.id.n_pos);
            shopName=itemView.findViewById(R.id.CCollect);
            price=itemView.findViewById(R.id.TextView514);

        }
    }
    //自定义 设置数据list
    public void setList(List<CourseBean> list){
        this.list=list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    //为每个item填充数据，设置点击事件
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate
                (R.layout.main_course_activity_item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        //取数据
        final CourseBean bean=list.get(position);
        if(bean==null)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;
        viewHolder.imageView.setImageResource(bean.getPic());
        viewHolder.foodName.setText(bean.getFoodname());
        viewHolder.shopName.setText(bean.getShopname());
        viewHolder.intro.setText(bean.getIntro());
        viewHolder.price.setText(bean.getPrice());
//        viewHolder.button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                list.remove(position);
//                notifyDataSetChanged();
//            }
//        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("---position="+position);
                Toast.makeText(context, ""+
                        bean.getFood_id()+
                        "      课程名："+bean.getFoodname()+
                        "     老师："+bean.getShopname()+
                        "     介绍："+bean.getIntro()+
                        "     价格:"+bean.getPrice(), Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(
                        context, CourseActivity.class);
                intent.putExtra("foodname",bean.getFoodname());
                intent.putExtra("shopname",bean.getShopname());
                intent.putExtra("foodintro",bean.getIntro());
                intent.putExtra("tel",bean.getTel());
                intent.putExtra("price",bean.getPrice());

                context.startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount() {
        if(list!=null)
            return list.size();
        else
            return 0;
    }
}
